<?php
/***
 * 描述：团购商品
 * @category		Mobile		
 * @package			Action			
 * @subpackage		
 * @author			wanght <whtaok@gmail.com>
 * @since			2013-4-6
 * @version			$Id: TuangouAction.class.php 91 2013-12-11 15:14:56Z whtaok@gmail.com $
 */
class TuangouAction extends MobileAction {
	/**
	 * 描述：团购商品列表
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function indexAct() {
		$search = array(
			'name'=>I('name'),
			'cate'=>I('cate')
		);
		$this->assign('search',$search);
		$this->display('index');
	}
	/**
	 * 描述：加载商品列表
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function listAct(){
		$search = array(
			'name'=>I('name'),
			'cate'=>I('cate')
		);
		list($list,$page) = TuangouModel::findListPage($search);
		$this->ajaxReturn(array('list'=>$list,'page'=>$page));
	}
	
	/**
	 * 描述：商品详细页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showDetailAct(){
		$detail = TuangouModel::findDetail(I('prod_id',0,''));
		$this->assign('detail',$detail);
		$this->display('detail');
	}
	
	/**
	 * 描述：商品内容页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showContentAct(){
		$rtnData = TuangouModel::findDetailContent(I('prod_id',0,''));
		$this->ajaxReturn($rtnData);
	}
	/**
	 * 描述：计算购买数量
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function updBuyNumAct(){
		$prod_id = I('prod_id');
		$num = intval(I('num'));
		if($num==0 || $num == false){
			$num = 1;
		}
		$rtnData = TuangouModel::updBuyNum($prod_id, $num);
		$this->ajaxReturn($rtnData);
	}
	
	/**
	 * 描述：显示订购页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showConfirmAct(){
		$this->checkAuth();
		$this->setPageTitle('预订商品');
		//配送地址信息
		$mod = new CommonModel('CustAddress');
		$sendList = $mod->setModWhere(array('cust_id'=>session(SES_CUST_ID)))->order('is_default desc')->select();
		if($sendList){
			$address = $sendList[0];
			$address['add_flag'] = 0; //地址存在
		}else{
			$cust = session(SES_CUST);
			$address['name']=$cust['name'];
			$address['mobile']=$cust['mobile'];
			$address['address']=cookie('cust_address');
			$address['add_flag'] = 1;//地址不存在，提交订单时添加地址
		}
		//配送时间：订购不需要指定时间
		$sendTime = get_shop_setting($this->curr_shop_id, '团购配送时间');
		if(!$sendTime){
			//如果没有在数据库中设定，取配置文件
			$sendTime = C('TUAN_SEND_TIME');
		}
		//订购的商品信息
		$prod_id = I('prod_id');
		$num = intval(I('num'));
		if(intval($num) == 0 ){
			$num = 1;
		}
		if($num > C('BUY_MAX_NUM')){
			$num = C('BUY_MAX_NUM');
			$this->assign('warning','该商品最多购买'.$num.'件');
		}
		if(empty($prod_id)){
			$this->error('商品不存在');
		}
		$detail = TuangouModel::findDetail($prod_id);
		$detail['total_amt'] = $num * $detail['price_real'];
		$detail['num'] = $num;
		$this->assign('address',$address);
		$this->assign('sendTime',$sendTime);
		$this->assign('itemDetail',$detail);
	
		$this->display('Tuangou/Order/confirm');
	}
	
	/**
	 * 描述：提交订单
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function doOrderAct(){
		$this->checkAuth();
		$shop_id = $this->curr_shop_id;
		$cust_id = $this->curr_cust_id;
		$address = array(
				'cust_id'=>$cust_id,
				'cust_name'=>I('cust_name'),
				'cust_mobile'=>I('cust_mobile'),
				'cust_address'=>I('cust_address'),
		);
		dump($cust_id);
		//判断是否保存地址信息到客户地址本中
		if(I('address_add_flag') == '1' ){
			$mod = new CustAddressModel();
			$address_num = $mod->setModWhere(array('cust_id'=>$cust_id))->count();
			if($address_num>0){
				//地址已存在
				cookie('cust_address',null);
			}else{
				//地址不存在
				$mod = new CustAddressModel();
				$mod->doAdd($address,1);
				cookie('cust_address',$address['cust_address']);
			}
		}
		//购物车商品校验
		$prod_id = I('prod_id');
		$num = intval(I('num'));
		if(intval($num) == 0 ){
			$this->error('商品数量不正确');
		}
		if(empty($prod_id)){
			$this->error('商品不存在');
		}
		$orderItem = TuangouModel::findDetail($prod_id);
		if(!$orderItem){
			$this->error('商品不存在');
		}
		$orderItem['num'] = $num;
		$orderItem['stock_prod_id'] = $prod_id;
		$orderItems[]=$orderItem;
		$orderPrice['total_amt'] = $num * $orderItem['price_real'];
		$orderPrice['send_fee'] = 0;	//免运费
		$orderTime = time();
		$orderCode = gen_order_code();
		$orderData = array(
				'order_code'=>$orderCode,
				'order_type'=>2,
				'order_status'=>1,
				'order_amt'=>$orderPrice['total_amt'],
				'order_send_fee'=>$orderPrice['send_fee'],
				'cust_id'=>$address['cust_id'],
				'cust_name'=>$address['cust_name'],
				'cust_mobile'=>$address['cust_mobile'],
				'cust_address'=>$address['cust_address'],
				'cust_free_time'=>I('cust_free_time'),
				'cust_order_status'=>0,
				'cust_order_time'=>$orderTime,
				'cust_bak'=>I('cust_bak'),
				'shop_id'=>$shop_id,
				'client_type'=>2,
				'crt_time'=>$orderTime,
				'crt_user'=>$cust_id,
				'upd_time'=>$orderTime,
				'upd_user'=>$cust_id,
		);
		$orderDetailData = array();
		foreach($orderItems as $key=>$value){
			$orderDetailData[] = array(
					'order_code' => $orderCode,
					'order_type' => 2,
					'prod_id' => $value['prod_id'],
					'prod_name' => $value['title'],
					'prod_spec' => $value['spec'],
					'prod_spec_qty' => $value['spec_qty'],
					'prod_spec_unit' => $value['spec_unit'],
					'prod_price_real' => $value['price_real'],
					'prod_price_sale' => $value['price_sale'],
					'prod_num' => $value['num'],
					'prod_stock_prod_id' => $value['stock_prod_id'],
					'cust_id' => $cust_id,
					'crt_time'=>$orderTime,
					'crt_user'=>$cust_id,
					'upd_time'=>$orderTime,
					'upd_user'=>$cust_id,
			);
		}
		$tranFlag = OrderModel::addOrder($orderData, $orderDetailData);
		if($tranFlag){
			//处理库存，处理失败不影响后续操作，但是生成一条错误记录提醒后台
			StockModel::updOrderStockTuangou($orderCode,$prod_id,$num);
			$this->redirect('Tuangou/showOrderSuccess',array('order_code'=>$orderCode));
		}else{
			$this->error('订单提交失败，请重新尝试');
		}
	}
	/**
	 * 描述：订单提交成功页面
	 * @access		public
	 * @author		wanght<whtaok@gmail.com>
	 * @since		2013-3-3
	 */
	public function showOrderSuccessAct(){
		$this->checkAuth();
		$this->setPageTitle('等待收货');
		$orderCode = I('order_code');
		$this->assign('order_code',$orderCode);
		$this->display('Tuangou/Order/result');
	}

}

?>